﻿using QQ群成员获取.Common;
using QQ群成员获取.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace QQ群成员获取.Operate
{
    public class ExcelExpotor : IExecute
    {
        private string strTabName;
        private List<GroupMemberModel> grpMembers;
        private string strSavePath = "";
        public ExcelExpotor(string strTabName, List<GroupMemberModel> grpMembers, string strSavePath)
        {
            this.strTabName = strTabName;
            this.grpMembers = grpMembers;
            this.strSavePath = strSavePath;
        }
        public bool DoSomeThing()
        {
            if (string.IsNullOrWhiteSpace(strTabName) || grpMembers == null || grpMembers.Count == 0)
            {
                return false;
            }
            DataTable dataTable = new DataTable() {
                TableName = strTabName.Replace("/", "").Replace(" ","")
            };

            FieldInfo[] arrInfo = typeof(GroupMemberModel).GetFields(BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);
            int iCoutn = arrInfo.Length;
            if (iCoutn < 1)
            {
                return false;
            }
            var dic = new Dictionary<string, string>();
            for (int i = 1; i <= iCoutn; i++)
            {
                FieldInfo pInfo = arrInfo[i - 1];
                string strKey = pInfo.Name.ToLower();
                foreach (Attribute item in pInfo.GetCustomAttributes(true))
                {
                    if (item is MemberModelAttribute)
                    {
                        string strChName = (item as MemberModelAttribute).ChName;
                        if (!string.IsNullOrWhiteSpace(strChName))
                        {
                            dic.Add(strKey, strChName);
                        }
                        break;
                    }
                }
            }
            if (dic.Count == 0)
            {
                return false;
            }
            foreach (var val in dic.Values)
            {
                if (dataTable.Columns[val] == null)
                {
                    dataTable.Columns.Add(val);
                }
            }
            foreach (var item in grpMembers)
            {
                DataRow dataRow = dataTable.NewRow();
                if (dic.ContainsKey(nameof(item.Index).ToLower()))
                {
                    dataRow[dic[nameof(item.Index).ToLower()]] = item.Index;
                }
                if (dic.ContainsKey(nameof(item.Nick).ToLower()))
                {
                    dataRow[dic[nameof(item.Nick).ToLower()]] = item.Nick;
                }
                if (dic.ContainsKey(nameof(item.Code).ToLower()))
                {
                    dataRow[dic[nameof(item.Code).ToLower()]] = item.Code;
                }
                if (dic.ContainsKey(nameof(item.Card).ToLower()))
                {
                    dataRow[dic[nameof(item.Card).ToLower()]] = item.Card;
                }
                if (dic.ContainsKey(nameof(item.Sex).ToLower()))
                {
                    dataRow[dic[nameof(item.Sex).ToLower()]] = item.Sex;
                }
                if (dic.ContainsKey(nameof(item.Ql).ToLower()))
                {
                    dataRow[dic[nameof(item.Ql).ToLower()]] = item.Ql;
                }
                if (dic.ContainsKey(nameof(item.Entertime).ToLower()))
                {
                    dataRow[dic[nameof(item.Entertime).ToLower()]] = item.Entertime;
                }
                if (dic.ContainsKey(nameof(item.Lasttime).ToLower()))
                {
                    dataRow[dic[nameof(item.Lasttime).ToLower()]] = item.Lasttime;
                }
                dataTable.Rows.Add(dataRow);
            }
            if (dataTable.Rows.Count == 0)
            {
                return false;
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dataTable);
            return new ExcelOper().GetExcelByDataSet(ds,strSavePath);
        }
    }
}
